﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using Webdisk;
using System.Runtime.InteropServices;

namespace Webdisk_New
{
    public partial class log_on_Form : Form
    {
        public log_on_Form()
        {
            InitializeComponent();
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
            Control.CheckForIllegalCrossThreadCalls = false;

        }
        private Point mPoint;



        //设置窗口外阴影
        private const int CS_DropSHADOW = 0x20000;
        private const int GCL_STYLE = (-26);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);



        private void log_on_Form_Load(object sender, EventArgs e)
        {


            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            //一开始，输入框的内容是灰色
            loginName_TextBox.ForeColor = Color.SlateGray;
            loginPass_TextBox.ForeColor = Color.SlateGray;
            string LoginMessage = "";
            string AutomaticMessage = "";
            string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            string filePath = ThePath + "\\..\\..\\LoginMessage\\LoginMessage.txt ";
            try
            {
                LoginMessage = File.ReadAllText(filePath);
            }
            catch
            {
                return;
            }

            try
            {
                if (LoginMessage.Substring(0, 19) == "RememberCheckedtrue")
                {
                    Remember.Checked = true;
                    loginName_TextBox.Text = File.ReadAllText(ThePath + "\\..\\..\\LoginMessage\\loginNameMessage.txt ");
                    loginPass_TextBox.Text = File.ReadAllText(ThePath + "\\..\\..\\LoginMessage\\loginPassMessage.txt ");
                }
            }
            catch
            {

                return;
            }

            try
            {
                if (File.ReadAllText(ThePath + "\\..\\..\\LoginMessage\\AutomaticMessage.txt ").Substring(0, 21) == "AutomaticMessagetrue")
                {
                    Automatic.Checked = true;
                }
            }
            catch
            {
                return;
            }


            if (Automatic.Checked == true)
            {
                Login();//登录

            }


        }

        
        private void log_on_Form_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);

        }

        private void log_on_Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        //用于储存图片名字
        private string Name;
        private void LoginPicName(string Name)
        {
            string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            login.Image = Image.FromFile(ThePath + "\\..\\..\\pic\\" + Name);
        }//登录按下封装方法

        private void login_MouseDown(object sender, MouseEventArgs e)//鼠标按下
        {
            Name = "登录按下.png";
            LoginPicName(Name);
        }

        private void login_MouseEnter(object sender, EventArgs e)//鼠标进入
        {
            Name = "登录经过.png";
            LoginPicName(Name);
        }

        private void login_MouseLeave(object sender, EventArgs e)//鼠标离开
        {
            Name = "登录初始.png";
            LoginPicName(Name);
        }

        private void login_MouseUp(object sender, MouseEventArgs e)//鼠标释放
        {
            Name = "登录初始.png";
            LoginPicName(Name);
        }

        private void MinPicName(string Name)
        {
            string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            Min.Image = Image.FromFile(ThePath + "\\..\\..\\pic\\" + Name);
        }//登录最小化封装方法

        private void Min_MouseDown(object sender, MouseEventArgs e)//鼠标按下
        {
            Name = "登录最小化按下.png";
            MinPicName(Name);
        }

        private void Min_MouseEnter(object sender, EventArgs e)//鼠标经过
        {
            Name = "登录最小化经过.png";
            MinPicName(Name);
        }

        private void Min_MouseLeave(object sender, EventArgs e)//鼠标离开
        {
            Name = "登录最小化初始.png";
            MinPicName(Name);
        }

        private void Min_MouseUp(object sender, MouseEventArgs e)//鼠标释放
        {
            Name = "登录最小化经过.png";
            MinPicName(Name);
        }

        private void ExitPicName(string Name)//登录关闭封装的方法
        {
            string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            Exit.Image = Image.FromFile(ThePath + "\\..\\..\\pic\\" + Name);
        }

        private void Exit_MouseDown(object sender, MouseEventArgs e)//鼠标按下
        {
            Name = "登录关闭按下.png";
            ExitPicName(Name);
        }

        private void Exit_MouseEnter(object sender, EventArgs e)//鼠标经过
        {
            Name = "登录关闭经过.png";
            ExitPicName(Name);
        }

        private void Exit_MouseLeave(object sender, EventArgs e)//鼠标离开
        {
            Name = "登录关闭初始.png";
            ExitPicName(Name);
        }

        private void Exit_MouseUp(object sender, MouseEventArgs e)//鼠标释放
        {
            Name = "登录关闭经过.png";
            ExitPicName(Name);
        }
        private void Min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;

        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();        
        }

        private void loginName_TextBox_TextChanged(object sender, EventArgs e)//内容被改变
        {
            loginName_TextBox.ForeColor = Color.Black;

        }

        private void loginName_TextBox_KeyUp(object sender, KeyEventArgs e)//释放键盘某键
        {
            if (loginPass_TextBox.ForeColor == Color.SlateGray && loginPass_TextBox.Text == "密码")
                loginPass_TextBox.Text = "";
        }

        private void loginName_TextBox_MouseDown(object sender, MouseEventArgs e)//鼠标按下
        {
            if (loginPass_TextBox.ForeColor == Color.SlateGray && loginPass_TextBox.Text == "密码")
            {
                loginPass_TextBox.Text = "";
            }

            if (loginName_TextBox.Text == "")
            {
                loginName_TextBox.Text = "用户名";
                loginName_TextBox.ForeColor = Color.SlateGray;
            }
        }

        private void loginPass_TextBox_TextChanged(object sender, EventArgs e)//内容被改变
        {
            loginPass_TextBox.ForeColor = Color.Black;
            if (loginPass_TextBox.Text!="密码")
            {
                loginPass_TextBox.ForeColor = Color.Black;
                loginPass_TextBox.PasswordChar = '*';
            }
        }


        private void Login()//登录
        {
            if (loginName_TextBox.Text == "" || loginName_TextBox.Text == "用户名" && loginName_TextBox.ForeColor == Color.SlateGray)
            {
                MessageBox.Show("请输入用户名");
            }
            else if (loginPass_TextBox.Text == "" || loginPass_TextBox.Text == "密码" && loginPass_TextBox.ForeColor == Color.SlateGray)
            {
                MessageBox.Show("请输入密码");
            }
            else
            {
                bool success = false;//记录登录是否成功
                DBHelper.InitConnection();//连接数据库
                SqlDataReader reader = null;
                reader = DBHelper.GetDataReader("select * from userInfo");//查询数据库
                while (reader.Read())//读取数据
                {
                    success = false;
                    string loginName = reader["loginName"].ToString();
                    string loginPass = reader["loginPass"].ToString();
                    string isadmin = reader["isadmin"].ToString();
                    if (loginName_TextBox.Text == loginName && loginPass_TextBox.Text == loginPass)//找到用户名和密码正确的用户，登录
                    {
                        //MessageBox.Show("登录成功");
                        Temp.TmeploginName = loginName_TextBox.Text;
                        Temp.Tempisadmin = isadmin;
                        UserHome HF = new UserHome();
                        HF.Show();
                        this.Hide();//隐藏登录窗口
                                    //MessageBox.Show("登录成功");




                        string ThePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
                        string LoginMessage = "";
                        string loginNameMessage = "";
                        string loginPassMessage = "";
                        string AutomaticMessage = "";
                        if (Remember.Checked == true)
                        {
                            LoginMessage = "RememberCheckedtrue";
                            loginNameMessage += loginName_TextBox.Text;
                            loginPassMessage += loginPass_TextBox.Text;
                        }
                        if (Automatic.Checked == true)
                        {
                            AutomaticMessage = "AutomaticMessagetrue";

                        }
                        File.WriteAllText(ThePath + "\\..\\..\\LoginMessage\\LoginMessage.txt ", LoginMessage);
                        File.WriteAllText(ThePath + "\\..\\..\\LoginMessage\\loginNameMessage.txt ", loginNameMessage);
                        File.WriteAllText(ThePath + "\\..\\..\\LoginMessage\\loginPassMessage.txt ", loginPassMessage);
                        File.WriteAllText(ThePath + "\\..\\..\\LoginMessage\\AutomaticMessage.txt ", AutomaticMessage);




                        success = true;
                        reader.Close();
                        DBHelper.Conn.Close();
                        break;
                    }
                }
                if (success == false)//有错误
                {
                    MessageBox.Show("用户名或密码有误", "错误");
                    reader.Close();
                    DBHelper.Conn.Close();
                }
                
            }



        }


        private void login_Click(object sender, EventArgs e)
        {
            try
            {
                Login();

            }
            catch
            {
                MessageBox.Show("网络连接超时，请检查网络连接！");

            }
        }

		private void loginName_TextBox_KeyDown(object sender, KeyEventArgs e)
		{

		}

		private void loginPass_TextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyValue == 13)
			{
				this.login.Focus();
				login_Click(sender, e);   //调用登录按钮的事件处理代码
			}
		}

		private void loginPass_TextBox_MouseDown(object sender, MouseEventArgs e)
		{
			if (loginPass_TextBox.ForeColor == Color.SlateGray && loginPass_TextBox.Text == "密码")
			{
				loginPass_TextBox.Text = "";
			}

			if (loginName_TextBox.Text == "")
			{
				loginName_TextBox.Text = "用户名";
				loginName_TextBox.ForeColor = Color.SlateGray;
			}
		}

		private void loginPass_TextBox_KeyUp(object sender, KeyEventArgs e)
		{
			if (loginPass_TextBox.ForeColor == Color.SlateGray && loginPass_TextBox.Text == "密码")
				loginPass_TextBox.Text = "";
		}

        private void log_on_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            Type.AnimateWindow(this.Handle, 300, Type.AW_BLEND | Type.AW_HIDE);
        }

		private void NewUser_Click(object sender, EventArgs e)
		{
			Encol encol = new Encol();
			encol.Show();
            this.Hide();

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
        }

        private void loginName_TextBox_Click(object sender, EventArgs e)
        {
            if (loginName_TextBox.Text=="用户名"&& loginName_TextBox.ForeColor == Color.SlateGray)
            {
                loginName_TextBox.Text = "";
            }
            if (loginPass_TextBox.Text=="")
            {
                loginPass_TextBox.Text = "密码";
                loginPass_TextBox.ForeColor = Color.SlateGray;

                loginPass_TextBox.PasswordChar = '\0';

            }
        }
        private void loginPass_TextBox_Click(object sender, EventArgs e)
        {
            if (loginPass_TextBox.Text == "密码")
            {
                loginPass_TextBox.PasswordChar = '\0';
                loginPass_TextBox.Text = "";
            }
            

        }
    }
}
